logo

The best IT Trainig Institute In Gurgaon

Test Annotations | BeforeGroups | BeforeClass

Before and After Class
  • @BeforeClass: This annotation is used to specify that a method should run before any of the test methods in the current class. Typically, it's used for setting up resources (e.g., opening a browser, setting up a database connection) that are needed for the entire test class.
  • @AfterClass: This annotation is used to specify that a method should run after all the test methods in the current class have been executed. It's commonly used to clean up resources (e.g., closing a browser, releasing database connections).
Before and After Groups
  • @BeforeGroups: This annotation is used to define a method that will be executed before any of the test methods in the specified group(s) are executed. It's often used to prepare the environment for a specific group of tests.
  • @AfterGroups: This annotation is used to define a method that will be executed after all the test methods in the specified group(s) are executed. It’s useful for cleanup activities related to the group.

Code for the Testing

package asc;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;



@Test(groups="user-regristration")
public class beforeClassAfterClass {

	
	
	
	@BeforeClass
	public void beforeClass()

	{

		System.out.println("Run this test before class");

	}
	
	
	@AfterClass
	public void afterClass()

	{

		System.out.println("Run This test After the class");

	}
	
	@BeforeGroups(value="regression")
	public void beforeGroups()

	{

		System.out.println("Run This Before Regression Groups");

	}

	@AfterGroups(value="regression")
	public void afterGroups()

	{

		System.out.println("Run This After Regression Groups");

	}
	
	@Test(priority = 1,groups="regression")
	public void Test1()

	{

		System.out.println("Test 1");

	}

	@Test(priority = 2,groups="regression")
	public void Test2() {

		System.out.println("Test 2");

	}

	
	@Test(groups={"regression","bvt"})
	public void Test3() {

		System.out.println("Test 3");

	}
	

	@Test(groups="bvt")
	public void Test4() {

		System.out.println("Test 4");

	}

}

            
  • @BeforeClass / @AfterClass: Run once before/after all methods in a test class.
  • @BeforeGroups / @AfterGroups: Run before/after methods belonging to specific groups.